package com.ctakit.sdk.app.widget.tab.home;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by naite.zhou on 16/5/11.
 */
public class HomeTabManager {

    private FragmentManager mFragmentManager;
    private HomeTabLayout.OnTabSelectedListener clientListener;
    private ConcurrentHashMap<String, HomeTabFragment> mFragments = new ConcurrentHashMap<>();
    private ConcurrentHashMap<Integer, HomeTabFragment> mFragmentMap = new ConcurrentHashMap<>();
    private String currentTag;
    private int mContainerId;
    private IHomeTabLayout mHomeTabLayout;


    private Handler handler = new Handler(Looper.getMainLooper());


    private HomeTabLayout.OnTabSelectedListener tabSelectedListener = new HomeTabLayout.OnTabSelectedListener() {
        @Override
        public boolean onBeforeSelect(int position) {
            return clientListener.onBeforeSelect(position);
        }

        @Override
        public void onSelected(int position, Bundle bundle) {
            if (clientListener != null) {
                onTabFragmentChanged(position, bundle);
                clientListener.onSelected(position, bundle);

            }
        }

        @Override
        public void onSelectedAgain(int position, Bundle bundle) {
            if (clientListener != null) {
                clientListener.onSelectedAgain(position, bundle);
            }
        }
    };

    private Context mContext;

    public HomeTabManager(FragmentManager mFragmentManager, Context context) {
        this.mFragmentManager = mFragmentManager;
        mContext = context;
    }

    public void setup(@IdRes int containerId,int selectIndex, IHomeTabLayout homeTabLayout,Bundle bundle) {
        if (homeTabLayout == null) {
            return;
        }
        if (homeTabLayout.getItemCount() <= 0) {
            return;
        }
        if (homeTabLayout.getOnTabSelectedListener() != null) {
            clientListener = homeTabLayout.getOnTabSelectedListener();
        }
        homeTabLayout.setOnTabSelectedListener(tabSelectedListener);
        mContainerId = containerId;
        mHomeTabLayout = homeTabLayout;
        mHomeTabLayout.setCurrent(selectIndex,bundle);
    }



    public HomeTabFragment getCurrentFragment() {
        return mFragments.get(currentTag);
    }

    private synchronized void  onTabFragmentChanged(final int position, final Bundle bundle) {


                FragmentTransaction ft = mFragmentManager.beginTransaction();
                Class tabFragmentClass = mHomeTabLayout.getTabFragmentClass(position);
                if (tabFragmentClass != null) {
                    String newTag = getFragmentTag(tabFragmentClass, position);
                    HomeTabFragment newFragment = mFragments.get(newTag);
                    if (newFragment == null) {
                        newFragment = (HomeTabFragment) mFragmentManager.findFragmentByTag(newTag);
                        if (newFragment == null) {
                            newFragment = (HomeTabFragment) Fragment.instantiate(mContext, tabFragmentClass.getName(), bundle);
                            ft.add(mContainerId, (Fragment) newFragment, newTag);
                        }
                        mFragments.put(newTag, newFragment);
                    }

                    if (mFragments != null && !mFragments.isEmpty()) {
                        Set<Map.Entry<String, HomeTabFragment>> framents = mFragments.entrySet();
                        for (Map.Entry<String, HomeTabFragment> mapEn : framents)

                            if (mapEn.getValue() != null) {
                                ft.hide((Fragment) mapEn.getValue());
                            }
                    }


                    ft.show((Fragment) newFragment);
                    currentTag = newTag;
                    ft.commitAllowingStateLoss();
                    if (newFragment.isAdd()&&((Fragment) newFragment).getActivity()!=null) {
                        newFragment.onFragmentResume();
                    }

                }



    }

    private String getFragmentTag(Class<HomeTabFragment> fragment, int position) {
        if (fragment == null) {
            return "";
        }
        String name = fragment.getSimpleName() + " #" + position;
        return name;
    }
}


